package org.uned.agonzalo16.bitacora.web.article;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.uned.agonzalo16.bitacora.dao.ArticleDao;
import org.uned.agonzalo16.bitacora.dao.BlogDao;
import org.uned.agonzalo16.bitacora.dao.UserDao;
import org.uned.agonzalo16.bitacora.domain.Article;
import org.uned.agonzalo16.bitacora.service.search.SearchService;
import org.uned.agonzalo16.bitacora.service.security.AuthenticatedUser;

@RequestMapping("/contributor/article")
@Controller
@SessionAttributes({ "userAttribute" })
public class ContributorArticleController {

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private BlogDao blogDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private SearchService searchService;

	@RequestMapping(method = RequestMethod.GET, value = "/beginCreate/{id}")
	public String beginCreate(@PathVariable("id") Long id, Model model) {
		model.addAttribute("article", new ArticleForm(id));
		return "contributor/article/create";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/create")
	public String create(@Valid @ModelAttribute("article") ArticleForm form, BindingResult result, @ModelAttribute("userAttribute") AuthenticatedUser user) {

		if (result.hasErrors()) {
			return "contributor/article/create";
		}

		Article article = new Article();
		article.setCreationDate(new Date());
		article.setOpenComments(true);
		article.setTitle(form.getTitle());
		article.setText(form.getText());
		article.setBlog(blogDao.get(form.getBlogId()));
		article.setUser(userDao.get(user.getId()));

		articleDao.merge(article);

		searchService.addArticle(article);

		return "redirect:/article/" + article.getId();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/beginUpdate/{id}")
	public String beginUpdate(@PathVariable("id") Long id, Model model) {
		model.addAttribute("article", new ArticleForm(articleDao.get(id)));
		return "contributor/article/update";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/update")
	public String update(@Valid @ModelAttribute("article") ArticleForm form, BindingResult result) {

		if (result.hasErrors()) {
			return "contributor/article/update";
		}

		Article article = articleDao.get(form.getId());
		article.setTitle(form.getTitle());
		article.setText(form.getText());

		articleDao.merge(article);

		searchService.addArticle(article);

		return "redirect:/article/" + article.getId();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/openComments/{id}")
	public String openComments(@PathVariable("id") Long id) {
		Article article = articleDao.get(id);
		article.setOpenComments(true);
		articleDao.merge(article);
		return "redirect:/contributor/article/beginUpdate/" + id;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/closeComments/{id}")
	public String closeComments(@PathVariable("id") Long id) {
		Article article = articleDao.get(id);
		article.setOpenComments(false);
		articleDao.merge(article);
		return "redirect:/contributor/article/beginUpdate/" + id;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public String delete(@RequestParam("id") Long id) {

		articleDao.delete(id);

		searchService.removeArticle(id);

		return "redirect:/";
	}
}
